package com.cdy.demo.java;

import java.io.IOException;
import java.io.InputStream;
import java.util.Map;

public class ProcessBuilderDemo {

    public static void main(String[] args) {
        //创建进程
        ProcessBuilder processBuilder = new ProcessBuilder("ipconfig","/all");
        //获取当前进程的环境变量
        Map<String, String> map = processBuilder.environment();
        Process process = null;
        try {
            //执行 ipconfig/all 命令并返回Process对象
            process = processBuilder.start();
        } catch (IOException e) {
            e.printStackTrace();
        }
        //获取进程标准输出流
        InputStream in = process.getInputStream();
        StringBuffer sb = new StringBuffer();
        int readbytes = -1;
        byte[] b = new byte[1024];
        try{
            while((readbytes = in.read(b)) != -1){
                sb.append(new String(b,0,readbytes));
            }
        }catch(IOException e1){
        }finally {
            try{
                in.close();
            }catch (IOException e2){
            }
        }
        System.out.println(sb.toString());
    }
}
